package com.gitee.minimalismstyle.fresh.common.data.redis.aspect

import com.gitee.minimalismstyle.fresh.common.data.redis.RedisUtils
import com.gitee.minimalismstyle.fresh.common.data.redis.annotation.Lock
import org.aspectj.lang.ProceedingJoinPoint
import org.aspectj.lang.annotation.Around
import org.aspectj.lang.annotation.Aspect
import org.redisson.api.RedissonClient
import org.springframework.stereotype.Component
import java.util.concurrent.TimeUnit

@Aspect
@Component
class LockAspect(val redisUtils: RedisUtils) {

    @Around("@annotation(lock)")
    fun around(pj: ProceedingJoinPoint, lock: Lock): Any? {
        var lockKey = lock.value
        if(lockKey.isEmpty()){
            val className = pj.target.javaClass.name
            val methodName = pj.signature.name
            lockKey = "${className}.${methodName}";
        }
        var obj: Any? = null
        redisUtils.lock(lockKey, lock.timeout){
            obj = pj.proceed()
        }
        return obj
    }
}